package com.example.easybuy.controller;

import com.example.easybuy.pojo.Products;
import com.example.easybuy.pojo.ShoppingCar;
import com.example.easybuy.pojo.User;
import com.example.easybuy.service.ShoppingCarService;
import com.example.easybuy.service.ShoppingCarServiceImpl;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class ShoppingCarController {
    private Logger logger = Logger.getLogger(ShoppingCarServiceImpl.class);

    @Autowired
    private ShoppingCarService shoppingCarService;

    /**
     * 添加到购物车
     * @param gid
     * @param uid
     * @param proNum
     */
    @RequestMapping("addShoppingCar")
    @ResponseBody
    @CrossOrigin
    public String addShoppingCar(String gid,String uid,String proNum){
        logger.debug("ShoppingCarController addShoppingCar gid: "+gid+" uid:  "+uid+" proNum: "+proNum);
        String isSuccess=shoppingCarService.addShoppingCar(gid,uid,proNum);
        logger.debug("ShoppingCarController addShoppingCar isSuccess: "+isSuccess);
        return isSuccess;
    }



    /**
     * 根据用户id查询下面的购物车商品
     * @param uid
     */
    @RequestMapping("shoppingCarUserById")
    @ResponseBody
    @CrossOrigin
    public List<Products> shoppingCarUserById(String uid){
        logger.debug("ShoppingCarController shoppingCarUserById uid:  "+uid);
        List<Products> productsList=shoppingCarService.shoppingCarUserById(uid);
        logger.debug("ShoppingCarController shoppingCarUserById uid:  "+uid+" result:" +productsList);
        return productsList;
    }


    /**
     * 根据用户id、根据购物车的商品id
     * 查 当前用户下一个购物车的数量
     */
    @RequestMapping("proNum")
    @ResponseBody
    @CrossOrigin
    public int proNum(String productsId, String uid){
        logger.debug("ShoppingCarController proNum  productsId: "+productsId+" uid: "+uid);
        int proNumeCount=shoppingCarService.proNum(productsId,uid);
        logger.debug("ShoppingCarController proNum  proNumeCount: "+proNumeCount);
        return proNumeCount;
    }



}
